package com.kalvan.log.logback.appender;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.rolling.RollingFileAppender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 自定义滚动输出appender,预留扩展
 *
 * @author chenliang
 */
public class ExtDebugRollingFileAppender<E> extends RollingFileAppender<E> {
    private static Boolean enable = null;

    @Override
    protected void append(E eventObject) {
        if (enable == null && eventObject != null) {
            LoggerContext lc = (LoggerContext) LoggerFactory
                    .getILoggerFactory();
            Level rootLevel = lc.getLogger(Logger.ROOT_LOGGER_NAME).getLevel();
            // 如果root 的日志=DEBUG 则启用
            // 如果root 的日志>DEBUG 则不启用
            enable = Level.DEBUG.equals(rootLevel);
        }
        if (enable) {
            super.append(eventObject);
        }
    }
}